package com.yidu.logisticssystem.service.impl;

import com.yidu.logisticssystem.dao.*;
import com.yidu.logisticssystem.entity.*;
import com.yidu.logisticssystem.entity.vo.CarriageVo;
import com.yidu.logisticssystem.entity.vo.StateVo;
import com.yidu.logisticssystem.service.LogisticsCarriageService;
import com.yidu.logisticssystem.utils.BaseMessage;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.ServletRequestDataBinder;
import org.springframework.web.bind.annotation.InitBinder;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.sql.Date;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

/**
 * 运输单(LogisticsCarriage)表服务实现类
 *
 * @author lzx
 * @since 2021-03-12 15:07:52
 */
@Service("logisticsCarriageService")
public class LogisticsCarriageServiceImpl implements LogisticsCarriageService {
    @Resource
    private LogisticsCarriageDao logisticsCarriageDao;


    @Resource
    private LogisticsBatchDao batchDao;

    @Resource
    private LogisticsCarriageDao carriageDao;

    @Resource
    private LogisticsStateDao stateDao;
    @Resource
    private LogisticsCarDao logisticsCarDao;

    @Resource
    private LogisticsCardriverDao logisticsCardriverDao;

    @Resource
    private LogisticsSiteDao siteDao;


    @Resource
    private LogisticsCarDao carDao;

    @Resource
    private  LogisticsCardriverDao cardriverDao;

    /**
     * 通过ID查询单条数据
     *
     * @param carriageId 主键
     * @return 实例对象
     */
    @Override
    public LogisticsCarriage queryById(Integer carriageId) {
        return this.logisticsCarriageDao.queryById(carriageId);
    }

    /**
     * 查询多条数据
     *
     * @param offset 查询起始位置
     * @param limit  查询条数
     * @return 对象列表
     */
    @Override
    public List<LogisticsCarriage> queryAllByLimit(int offset, int limit) {
        return this.logisticsCarriageDao.queryAllByLimit(offset, limit);
    }

    /**
     * 新增数据
     *
     * @param logisticsCarriage 实例对象
     * @return 实例对象
     */
    @Override
    public LogisticsCarriage insert(LogisticsCarriage logisticsCarriage) {
        this.logisticsCarriageDao.insert(logisticsCarriage);
        return logisticsCarriage;
    }

    /**
     * 修改数据
     *
     * @param logisticsCarriage 实例对象
     * @return 实例对象
     */
    @Override
    public LogisticsCarriage update(LogisticsCarriage logisticsCarriage) {
        this.logisticsCarriageDao.update(logisticsCarriage);
        return this.queryById(logisticsCarriage.getCarriageId());
    }

    /**
     * 通过主键删除数据
     *
     * @param carriageId 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Integer carriageId) {
        return this.logisticsCarriageDao.deleteById(carriageId) > 0;
    }

    /**
     * 发货
     * @param vo
     * @param request
     * @return
     */
    @Override
    @Transactional
    public int fahuo(StateVo vo, HttpServletRequest request) {

        LogisticsStaff staff = (LogisticsStaff)request.getSession().getAttribute("staff");//取得session staff对象
        String areaid=staff.getMapId(); //定义地区id
        String bh= UUID.randomUUID().toString().substring(0,10);//

        List<LogisticsBatch> batchs=new ArrayList<>();  // 批量订单集合
        List<LogisticsState> states = new ArrayList<>(); // 货运状态集合
        for (String val:vo.getHydid()) {    //根据vo 中数组循环
            LogisticsBatch batch=new LogisticsBatch(); // 订单批次对象
            batch.setWayNum(val);
            batch.setBatchNum(bh);
            batchs.add(batch);

            LogisticsState state=new LogisticsState(); // 货运状态 对象
            state.setDetailtypeId(4);
            state.setStaffId(staff.getStaffId());
            state.setStaTai(0);
            state.setStateTime(new Date(System.currentTimeMillis()));//取得当前时间new Date(System.currentTimeMillis())
            state.setWayNum(val);
            state.setStateRemark(vo.getStateRemark());
            states.add(state);
        }
        stateDao.insertyszt(states);//运输状态添加
        batchDao.insertfind(batchs);//运输批次添加
        LogisticsCarriage carriage=new LogisticsCarriage();//货运单
        carriage.setBatchNum(bh);
        carriage.setCarNumber(vo.getCph());
        carriage.setCarriageArrival(null);
        carriage.setCarriageDestination(vo.getShdz());
        carriage.setCarriageRemark(vo.getWayRemark());
        carriage.setCarriageStarttime(new Date(System.currentTimeMillis()));// 现在时间 System.currentTimeMillis()
        carriage.setCarriageStart(areaid);
        carriage.setCarSjid(vo.getSjId());
        carriageDao.insert(carriage);//运输单添加
        //修改车辆状态
        LogisticsCar  car=new LogisticsCar();
        car.setCarNum(vo.getCph());
        car.setCarTime(1);
        carDao.updateCarNum(car);
        //修改司机状态 LogisticsCardriver
        LogisticsCardriver cardriver=new LogisticsCardriver();
        cardriver.setStaffId(vo.getSjId());
        cardriver.setDriverTime(1);
        cardriverDao.update(cardriver);
        //修改发货状态 updatefahuo
        return  stateDao.updatefahuo(vo.getHydid());
    }

    @Override
    @Transactional
    public int paisong(StateVo vo, HttpServletRequest httpSession) {
        LogisticsStaff staff = (LogisticsStaff)httpSession.getSession().getAttribute("staff");
        String areaid=staff.getMapId(); //地区编号
        String bh= UUID.randomUUID().toString().substring(0,10);//uuid随机数取0-10

        List<LogisticsBatch> list1=new ArrayList<>();
        List<LogisticsState> list2 = new ArrayList<>();
        for (String val:vo.getHydid()) {
            //订单批次
            LogisticsBatch batch=new LogisticsBatch();
            batch.setWayNum(val);
            batch.setBatchNum(bh);
            list1.add(batch);

            //货运状态增加
            LogisticsState state=new LogisticsState();
            state.setDetailtypeId(5);
            state.setStaffId(vo.getSjId());
            state.setStaTai(0);
            state.setStateTime(new Date(System.currentTimeMillis()));
            state.setWayNum(val);
            state.setStateRemark(vo.getStateRemark());
            list2.add(state);
        }
        stateDao.insertyszt(list2);
        batchDao.insertfind(list1);//运输批次
        LogisticsCarriage carriage=new LogisticsCarriage();
        carriage.setBatchNum(bh);
        carriage.setCarNumber(null);
        carriage.setCarriageArrival(null);
        carriage.setCarriageDestination("本地送达");//目的地
        carriage.setCarriageRemark(vo.getWayRemark());
        carriage.setCarriageStarttime(new Date(System.currentTimeMillis()));
        carriage.setCarriageStart(areaid);//运输单出发地
        carriage.setCarSjid(vo.getSjId());
        carriageDao.insert(carriage);//运输单
        //货运状态

        return  stateDao.updateps(vo.getHydid());
    }




    @Override
    public Integer insertkdysd(StateVo vo, HttpServletRequest request) {
        LogisticsStaff staff = (LogisticsStaff)request.getSession().getAttribute("staff");

        List<LogisticsState> list2 = new ArrayList<>();
        for (String val:vo.getHydid()) {
            //状态增加
            LogisticsState state=new LogisticsState();
            state.setDetailtypeId(6);
            state.setStaffId(staff.getStaffId());
            state.setStaTai(1);
            state.setStateTime(new Date(System.currentTimeMillis()));
            state.setWayNum(val);
            state.setStateRemark(vo.getStateRemark());
            list2.add(state);
        }
        stateDao.insertyszt(list2);
        return  stateDao.updatesd(vo.getHydid());
    }

    /**
     * 查询司机个人的运单
     * @param carriageVo 实例对象
     * @return
     */
    @Override
    public List<CarriageVo> queryAll2(CarriageVo carriageVo) {
        return this.carriageDao.queryAll2(carriageVo);
    }

    /**
     * 修改到达时间
     * @param batchNum
     * @param id
     * @param carNum
     * @param carSjid
     * @return
     */
    @Override
    @Transactional
    public int updateTime(String batchNum,String id,String carNum,String carSjid,String carriageDestination,String carriageStart) {
        LogisticsCarriage logisticsCarriage = new LogisticsCarriage();
        logisticsCarriage.setCarSjid(Integer.parseInt(carSjid));//赋值司机ID
        logisticsCarriage.setCarNumber(carNum);//赋值车牌
        logisticsCarriage.setCarriageDestination(carriageDestination);//赋值目的地
        logisticsCarriage.setCarriageStart(carriageStart);//赋值出发地
        logisticsCarriage.setCarriageId(Integer.parseInt(id));//赋值运输单id
        logisticsCarriage.setBatchNum(batchNum);//赋值批次编号
        logisticsCarriage.setCarriageRemark("已送达");//修改备注，“已送达”
        //获得当前日期
        SimpleDateFormat simpleDateFormat=new SimpleDateFormat();
        //格式化日期
        simpleDateFormat.applyPattern("yyyy-MM-dd");
        java.util.Date date = new java.util.Date();
        //赋值到达时间为当前日期
        logisticsCarriage.setCarriageArrival(java.sql.Date.valueOf(simpleDateFormat.format(date)));
        //修改到达时间
        logisticsCarriageDao.updateTime(logisticsCarriage);
        //修改司机状态
        LogisticsCardriver logisticsCardriver = new LogisticsCardriver();
        logisticsCardriver.setStaffId(logisticsCarriage.getCarSjid());//得到司机Id
        //判断如果为本地送达，将司机地址改为出发地点,否则改为目的地地点
        if ("本地送达".equals(logisticsCarriage.getCarriageDestination())){
            logisticsCardriver.setMapId(logisticsCarriage.getCarriageStart());
        }else {
            logisticsCardriver.setMapId(logisticsCarriage.getCarriageDestination());
        }
        //将司机状态改为0(默认为空闲)
        logisticsCardriver.setDriverTime(0);
        logisticsCardriverDao.updateCarId(logisticsCardriver);
        //修改车辆状态
        LogisticsCar logisticsCar = new LogisticsCar();
        logisticsCar.setCarNum(logisticsCarriage.getCarNumber());//得到车牌号
        //判断如果为本地送达，将车辆地址改为出发地点,否则改为目的地地点
        if ("本地送达".equals(logisticsCarriage.getCarriageDestination())){
            logisticsCar.setMapId(logisticsCarriage.getCarriageStart());
        }else {
            logisticsCar.setMapId(logisticsCarriage.getCarriageDestination());
        }
        //将车辆状态改为默认0，空闲
        logisticsCar.setCarTime(0);
        logisticsCarDao.updateCarNum(logisticsCar);

        String[] strings = batchDao.selectWayNum(logisticsCarriage.getBatchNum());
        stateDao.updatepsys(strings);
        return this.carriageDao.updateTime(logisticsCarriage);
    }

    @InitBinder
    public void initBinder(ServletRequestDataBinder binder) {
        binder.registerCustomEditor(java.util.Date.class, new CustomDateEditor(new SimpleDateFormat("yyyy-MM-dd"),
                true));
    }
}
